<template>
  <div>
    <van-nav-bar title="个人信息" left-arrow @click-left="$router.go(-1)" />
    <input
      type="file"
      ref="input"
      hidden
      accept=".png,.jpg"
      @change="onSelected"
    />
    <van-cell is-link title="头像" @click="$refs.input.click()">
      <van-image round width="0.8rem" height="0.8rem" :src="userInfo.photo" />
    </van-cell>
    <van-cell is-link title="昵称" @click="isShowNcame = true">
      <span>{{ userInfo.name }}</span>
    </van-cell>
    <van-cell is-link title="性别" @click="isShowGender = true">
      <span>{{ userInfo.gender === 0 ? '男' : '女' }}</span>
    </van-cell>
    <van-cell is-link title="生日" @click="isShowBirthday = true">
      <span>{{ userInfo.birthday }}</span>
    </van-cell>
    <!-- 弹出层 -->
    <!-- 更新头像的弹层 -->
    <van-popup
      v-model="isShowAvatar"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <UpdateAvatar
        :photo="photo"
        @close="isShowAvatar = false"
        v-if="isShowAvatar"
        @updata-photo=";[(userInfo.photo = $event), (isShowAvatar = false)]"
      ></UpdateAvatar>
    </van-popup>
    <!-- 更新昵称的弹层 -->
    <van-popup
      v-model="isShowNcame"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <UdateNickname
        v-if="userInfo.name && isShowNcame"
        :name.sync="userInfo.name"
        @close="isShowNcame = false"
      ></UdateNickname>
    </van-popup>
    <!-- 更新昵称的弹层 -->
    <van-popup
      v-model="isShowGender"
      position="bottom"
      :style="{ height: '30%' }"
    >
      <UpdateGender
        v-if="userInfo.name && isShowGender"
        :gender.sync="userInfo.gender"
        @close="isShowGender = false"
      ></UpdateGender>
    </van-popup>
    <!-- 更新生日的弹层 -->
    <van-popup
      v-model="isShowBirthday"
      position="bottom"
      :style="{ height: '30%' }"
    >
      <!-- <UpdateBirthday
      v-if="userInfo.birthday && isShowBirthday"
      @close="isShowBirthday = false"
      :birthday.sync="userInfo.birthday"></UpdateBirthday> -->
      <UpdateBirthday
        v-if="userInfo.birthday && isShowBirthday"
        @close="isShowBirthday = false"
        v-model="userInfo.birthday"
      ></UpdateBirthday>
    </van-popup>
    <!-- 弹出层 -->
  </div>
</template>

<script>
import { resolveToBase64 } from '@/utils/index'
import UpdateGender from './componemts/UpdateGender.vue'
import UdateNickname from './componemts/UdateNickname.vue'
import UpdateBirthday from './componemts/UpdateBirthday.vue'
import UpdateAvatar from './componemts/UpdateAvatar.vue'
import { getUserProfileAPI } from '@/api'
export default {
  name: '',
  components: {
    UdateNickname,
    UpdateGender,
    UpdateBirthday,
    UpdateAvatar
  },
  props: {},
  data() {
    return {
      userInfo: {},
      isShowNcame: false,
      isShowGender: false,
      isShowBirthday: false,
      isShowAvatar: false,
      photo: ''
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getUserProfile()
  },
  methods: {
    async getUserProfile() {
      const { data } = await getUserProfileAPI()
      // console.log(data)
      this.userInfo = data.data
    },
    async onSelected() {
      // 1.获取用户选择的文件（保存在input.files）
      const file = this.$refs.input.files[0]
      // 2.将file对象转成base64
      const base64 = await resolveToBase64(file)
      // console.log(base64)
      this.photo = base64
      // 3.打开弹出层
      this.isShowAvatar = true
      // 4.手动将input.value清空
      this.$refs.input.value = ''
    }
  }
}
</script>

<style scoped></style>
